<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class articles_model extends CI_Model{
    function __construct(){
        parent::__construct();
    }
    
    public function save($data){
        $articlecontents = $data['articlecontents'];
        $articlecontents = $this->db->escape($articlecontents);
        $category = $data['category'];
        $userid = $data['userid'];
		$articleid = $data['articleid'];

        if($articleid == '0') {
			// Insert
			$sql = 'INSERT INTO articles (userid, article,categoryid,createdon,lastmodifiedon) VALUES (?,' . $articlecontents . ',\'' . $category . '\',NOW(),NOW())';
			$this->db->query($sql,$userid);
		} else {
			// Update
			$sql = 'UPDATE articles SET article = ' . $articlecontents . ',categoryid = ?, lastmodifiedon = NOW() WHERE id = ?';
			$this->db->query($sql,array($category, $articleid));
		}
        
		return $this->db->affected_rows();
    }

    function getArticles($category) {
        $sql = "SELECT a.id, LEFT(a.article,200) as article, a.categoryid, a.createdon, a.lastmodifiedon,a.cnt_view,
		a.cnt_likes, u.uname FROM articles a LEFT JOIN users u ON a.userid=u.id WHERE a.deleted = '0'";

        if($category != "0") {
            $sql = $sql . ' AND a.categoryid = ?';
        }
        $sql = $sql . ' ORDER BY a.id DESC';        

        $query = $this->db->query($sql, $category);
        
        return $query->result();
    }

    function getArticleForID($id) {
        if(isset($id)) {
            // Increment post view counter by 1
            $sql = "UPDATE articles SET cnt_view = cnt_view + 1 WHERE id = ?";
            $this->db->query($sql, $id);

            // Get post details
            $sql = "SELECT a.id, a.article, a.categoryid, a.createdon,a.lastmodifiedon,a.cnt_view,a.cnt_likes, u.uname FROM articles a LEFT JOIN users u ON a.userid=u.id WHERE a.deleted = '0' ";
            $sql = $sql . ' AND a.id = ?';
            $query = $this->db->query($sql, $id);
                    
            return $query->result();
        } else {
            return null;
        }        
    }
	
	function updateLike($userid, $articleid) {
		if((isset($userid)) && (isset($articleid))) {
			$this->db->from('articlelikes');
			$this->db->where('articleid', $articleid);
			$this->db->where('userid', $userid);
			if($this->db->count_all_results() == 0) {
				$sql = "INSERT INTO articlelikes (userid, articleid) VALUES (?,?)";
				$query = $this->db->query($sql, array($userid, $articleid));
				
				$sql = "UPDATE articles SET cnt_likes = cnt_likes + 1 WHERE id = ?";
				$query = $this->db->query($sql, $articleid);
			} else {
				$sql = "DELETE FROM articlelikes WHERE userid = ? AND articleid = ?";
				$query = $this->db->query($sql, array($userid, $articleid));
				
				$sql = "UPDATE articles SET cnt_likes = cnt_likes - 1 WHERE id = ?";
				$query = $this->db->query($sql, $articleid);
			}
			
			$this->db->select('cnt_likes');
			$this->db->from('articles');
			$this->db->where('id', $articleid);
			$query = $this->db->get();
			return $query->result();
		} else {
			return 1;
		}
	}
}
?>